from mako.template import Template
from mako.lookup import TemplateLookup
import json

class Tpl:
    def __init__(self):
        self.data = {}
        self.tpl_name = ""
        self._json = False
        
    def set_tpldir(self, dirname):
        self.tpl_dir = dirname
    
    def add_data(self, name, data):
        self.data[name] = data

    def set_tpl(self, filename):
        self.tpl_name = filename
        
    def set_json(self, is_json_set):
        self._json = is_json_set
        
    def out(self):
        if self._json:
            return json.dumps(self.data)
        else:
            self.output = Template(filename = self.tpl_dir + self.tpl_name, module_directory = self.tpl_dir + "template_c").render(data = self.data)
            return self.output